package com.arvato.minipro.controller.admin.mall;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.arvato.minipro.bean.bean_do.mall.EcEffectDo;
import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_dto.common.Result;
import com.arvato.minipro.bean.bean_vo.mall.EcEffectVo;
import com.arvato.minipro.service.mall.EffectService;

/**
 * @author: smallking
 * 
 * @Descibe:后台管理商品功能的Controller
 * 
 * @Date: 2018年4月24日  下午4:55:36
 */
@Controller
@RequestMapping(value = "/admin/mall/effect")
public class AdminEffectController {
	
	@Autowired
	private EffectService effectService;
	
	/**
	 * 查询商品分类列表
	 * @param commodityId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/list", method = {RequestMethod.POST})
	public PageData list(String commodityId){
		Query query = new Query();
		PageData pageData = effectService.listEffectByQuery(query, query.getPageNum(), query.getPageSize());
		if(commodityId != null){
			List<?> data = pageData.getData();
			// 如果commodityId不是空 那么给commodityId的分类打上勾
			query.put("commodityId", commodityId);
			List<EcEffectVo> effectList = effectService.listEffectByQuery(query);
			for(EcEffectVo effectVo : effectList){
				for(Object obj : data){
					EcEffectVo effect = (EcEffectVo)obj;
					if(effect.getEffectId().equals(effectVo.getEffectId())){
						effect.setChecked(true);
						break;
					}
				}
			}
		}
		return pageData;
	}
	
	/**
	 * 根据ID查询商品分类
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/detail", method = {RequestMethod.POST})
	public Result detail(String effectId){
		Result result = new Result(true);
		EcEffectVo effectVo = effectService.getEffectById(effectId);
		result.setData(effectVo);
		return result;
	}
	
	/**
	 * 新增一个商品分类
	 * @param EffectName
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/insert", method = {RequestMethod.POST})
	public Result insert(EcEffectDo effectDo){
		Result result = new Result(false);
		try {
			int row = effectService.insert(effectDo);
			if(row == 1){
				result.setResult(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	
	@ResponseBody
	@RequestMapping(value="/update", method = {RequestMethod.POST})
	public Result update(EcEffectDo effectDo){
		Result result = new Result(false);
		try {
			int row = effectService.update(effectDo);
			if(row == 1){
				result.setResult(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * 删除一个地区
	 * @param EffectName
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete", method = {RequestMethod.POST})
	public Result delete(String effectId){
		Result result = new Result(false);
		try {
			int row = effectService.delete(effectId);
			if(row == 1){
				result.setResult(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
